<script setup>
const props = defineProps({
  error: {
    type: Object,
    default: () => ({}),
  },
})

// 错误类型映射
const errorConfig = computed(() => {
  const statusCode = props.error?.statusCode || 500

  const configs = {
    404: {
      title: '页面未找到',
      message: '抱歉，您访问的页面不存在或已被删除',
      emoji: '🔍',
      buttonText: '返回首页',
    },
    500: {
      title: '服务器错误',
      message: '抱歉，服务器出现了一些问题，请稍后再试',
      emoji: '⚠️',
      buttonText: '重新加载',
    },
    403: {
      title: '访问被拒绝',
      message: '抱歉，您没有权限访问此页面',
      emoji: '🚫',
      buttonText: '返回首页',
    },
  }

  return configs[statusCode] || configs[500]
})

const handleAction = () => {
  if (props.error?.statusCode === 500) {
    clearError({ redirect: '/' })
  } else {
    navigateTo('/')
  }
}
</script>

<template>
  <div class="error-page">
    <div class="error-container">
      <!-- 错误图标 -->
      <div class="error-icon">
        <span class="emoji">{{ errorConfig.emoji }}</span>
        <div class="icon-bg"></div>
      </div>

      <!-- 错误信息 -->
      <div class="error-content">
        <h1 class="error-code">{{ props.error?.statusCode || 500 }}</h1>
        <h2 class="error-title">{{ errorConfig.title }}</h2>
        <p class="error-message">{{ errorConfig.message }}</p>

        <!-- 详细错误信息（仅开发环境） -->
        <div v-if="props.error?.message" class="error-detail">
          <p class="detail-label">错误详情：</p>
          <p class="detail-text">{{ props.error.message }}</p>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="error-actions">
        <a-button type="primary" size="large" @click="handleAction">
          {{ errorConfig.buttonText }}
        </a-button>
        <a-button size="large" @click="() => navigateTo(-1)"> 返回上一页 </a-button>
      </div>

      <!-- 装饰元素 -->
      <div class="decorations">
        <div class="circle circle-1"></div>
        <div class="circle circle-2"></div>
        <div class="circle circle-3"></div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.error-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  position: relative;
  overflow: hidden;
}

.error-container {
  background: white;
  border-radius: 24px;
  padding: 60px 40px;
  max-width: 600px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  position: relative;
  z-index: 1;
}

/* 错误图标 */
.error-icon {
  position: relative;
  width: 120px;
  height: 120px;
  margin: 0 auto 32px;
}

.emoji {
  font-size: 80px;
  position: relative;
  z-index: 2;
  display: block;
  animation: bounce 2s infinite;
}

.icon-bg {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  border-radius: 50%;
  z-index: 1;
}

@keyframes bounce {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-20px);
  }
}

/* 错误内容 */
.error-content {
  margin-bottom: 40px;
}

.error-code {
  font-size: 72px;
  font-weight: 800;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 0 0 16px 0;
  line-height: 1;
}

.error-title {
  font-size: 28px;
  font-weight: 700;
  color: #1a202c;
  margin: 0 0 12px 0;
}

.error-message {
  font-size: 16px;
  color: #718096;
  line-height: 1.6;
  margin: 0;
}

/* 错误详情 */
.error-detail {
  margin-top: 24px;
  padding: 16px;
  background: #f7fafc;
  border-radius: 12px;
  border-left: 4px solid #667eea;
  text-align: left;
}

.detail-label {
  font-size: 14px;
  font-weight: 600;
  color: #4a5568;
  margin: 0 0 8px 0;
}

.detail-text {
  font-size: 13px;
  color: #718096;
  margin: 0;
  font-family: monospace;
  word-break: break-all;
}

/* 操作按钮 */
.error-actions {
  display: flex;
  gap: 16px;
  justify-content: center;
}

.error-actions :deep(.ant-btn-primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  height: 48px;
  padding: 0 32px;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.error-actions :deep(.ant-btn-primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

.error-actions :deep(.ant-btn:not(.ant-btn-primary)) {
  height: 48px;
  padding: 0 32px;
  font-weight: 600;
  border: 2px solid #e2e8f0;
  color: #4a5568;
}

.error-actions :deep(.ant-btn:not(.ant-btn-primary):hover) {
  border-color: #667eea;
  color: #667eea;
}

/* 装饰元素 */
.decorations {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: hidden;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 20s infinite;
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -150px;
  left: -150px;
  animation-delay: 0s;
}

.circle-2 {
  width: 200px;
  height: 200px;
  bottom: -100px;
  right: -100px;
  animation-delay: 5s;
}

.circle-3 {
  width: 150px;
  height: 150px;
  top: 50%;
  right: -75px;
  animation-delay: 10s;
}

@keyframes float {
  0%,
  100% {
    transform: translate(0, 0) scale(1);
  }
  33% {
    transform: translate(30px, -30px) scale(1.1);
  }
  66% {
    transform: translate(-20px, 20px) scale(0.9);
  }
}

/* 响应式 */
@media (max-width: 640px) {
  .error-container {
    padding: 40px 24px;
  }

  .error-code {
    font-size: 56px;
  }

  .error-title {
    font-size: 24px;
  }

  .error-message {
    font-size: 15px;
  }

  .error-icon {
    width: 100px;
    height: 100px;
  }

  .emoji {
    font-size: 64px;
  }

  .error-actions {
    flex-direction: column;
  }

  .error-actions :deep(.ant-btn) {
    width: 100%;
  }
}
</style>
